[#ftl]
<html>
  <head>
    <title>AVM Version Purge</title>
    <link rel="stylesheet" href="${url.context}/css/main.css" TYPE="text/css">
    <script type="text/javascript" src="http://datejs.googlecode.com/files/date.js"></script>
    <script type="text/javascript">
      function dateClicked()
      {
        var form         = document.getElementById("purgeVersionsForm");
        var errorMessage = document.getElementById("versionMessage");

        form.version.disabled    = true;
        form.version.value       = "";
        errorMessage.textContent = "";
        form.date.disabled       = false;
      }

      function versionClicked()
      {
        var form          = document.getElementById("purgeVersionsForm");
        var errorMessage  = document.getElementById("dateMessage");
        var formattedDate = document.getElementById("formattedDate");

        form.version.disabled     = false;
        form.date.disabled        = true;
        form.date.value           = "";
        errorMessage.textContent  = "";
        formattedDate.textContent = "";
      }

      function getRadioValue(radio)
      {
        var result = "";

        for (var i = 0; i < radio.length; i++)
        {
          if (radio[i].checked)
          {
            result = radio[i].value;
          }
        }

        return result;
      }

      function validateRequired(field, errorMessageElement, errorMessage)
      {
        var result = true;

        if (field.value == null || field.value == "")
        {
          errorMessageElement.textContent = errorMessage;
          result                          = false;
        }

        return result;
      }

      function isNumeric(value)
      {
        var result = /^\-?(\d)+$/.test(value);

        return result;
      }

      function isPositiveInteger(value)
      {
        var result = false;

        if (isNumeric(value))
        {
          result = value > 0;
        }

        return result;
      }

      function validatePositiveInteger(field, errorMessageElement, errorMessage)
      {
        var result = true;

        if (!isPositiveInteger(field.value))
        {
          errorMessageElement.textContent = errorMessage;
          result                          = false;
        }

        return result;
      }

      function isDate(value)
      {
        var result = false;
        var date   = Date.parse(value);

        if (date != null)
        {
          result = true;
        }

        return result;
      }

      function validateDate(field, errorMessageElement, errorMessage)
      {
        var result = true;

        if (!isDate(field.value))
        {
          errorMessageElement.textContent = errorMessage;
          result                          = false;
        }

        return result;
      }

      function validateForm(form)
      {
        var result = true;

        // Reset error messages
        document.getElementById("avmStoreMessage").textContent = "";
        document.getElementById("versionMessage").textContent  = "";
        document.getElementById("dateMessage").textContent     = "";

        // Now validate each field
        result = validateRequired(form.avmStore, document.getElementById("avmStoreMessage"), "Web Project is mandatory.") && result;

        if (getRadioValue(form.purgeType) == "version")
        {
          result = validatePositiveInteger(form.version, document.getElementById("versionMessage"), "Version number is mandatory, and must be a positive integer.") && result;
        }
        else
        {
          result = validateDate(form.date, document.getElementById("dateMessage"), "Date is mandatory, and must be a valid date.") && result;
        }

        if (result)
        {
          form.isoDate.value = Date.parse(form.date.value).toString("yyyy-MM-ddTHH:mm:ssZ");
        }

        return result;
      }

      function parseDate(input)
      {
        var date = Date.parse(input.value);

        document.getElementById("dateMessage").textContent = "";

        if (date != null)
        {
          document.getElementById("formattedDate").textContent = date.toLocaleString();
        }
        else
        {
          document.getElementById("formattedDate").textContent = "Invalid date.";
        }
      }
    </script>
  </head>
  <body>
    <table>
      <tr>
        <td><img src="${url.context}/images/logo/AlfrescoLogo32.png" alt="Alfresco" /></td>
        <td><nobr>AVM Version Purge</nobr></td>
      </tr>
      <tr><td><td>Alfresco ${server.edition} v${server.version}
    </table>
    <hr/>
  [#if webProjects?size > 0]
    <form id="purgeVersionsForm" action="${url.service}" method="POST" onSubmit="return validateForm(this)">
      <table>
        <tr>
          <td align="right">Web Project:</td>
          <td align="left">
            <select name="avmStore">
    [#list webProjects as webProject]
              <option value="${webProject.avmStore}">${webProject.name}</option>
    [/#list]
            </select>
          </td>
          <td id="avmStoreMessage" style="color:red"></td></tr>
        <tr>
          <td align="right">Purge all before:</td>
          <td align="left">
            <table>
              <tr>
                <td align="left"><input type="radio" name="purgeType" value="version" onclick="versionClicked()" checked /> version:</td>
                <td align="left"><input type="text" name="version" /></td>
                <td id="versionMessage" style="color:red" ></td>
              </tr>
              <tr>
                <td align="left"><input type="radio" name="purgeType" value="date" onclick="dateClicked()" /> date:</td>
                <td align="left"><input type="text" name="date" onblur="parseDate(this)" disabled /> <input type="hidden" name="isoDate" /></td>
                <td id="dateMessage" style="color:red"></td>
              </tr>
              <tr>
                <td align="left">&nbsp;</td>
                <td id="formattedDate" align="left" colspan="2">&nbsp;</td>
              </tr>
            </table>
          </td>
        </tr>
        <tr><td colspan="3"><input type="submit" name="submit" value="Purge Versions" /></td></tr>
      </table>
    </form>
  [#else]
    <p>You do not have any Web Projects configured.</p>
  [/#if]
  </body>
</html>
